﻿Imports ISNet.WebUI.WebGrid
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports log4net

Partial Class mtProducts
    Inherits System.Web.UI.Page

    Private Shared logger As ILog = LogManager.GetLogger("mtProducts")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        ScreenName.Text = "ระบบจัดทำงบประมาณ  >>  ตั้งค่าระบบ >>  ผลิตภัณฑ์"
        'Session("users") = "bb0011"
        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        
        If IsPostBack = False Then
            PlanYearsTxt.Text = Session("PlanYear")
            Session("PlanYearTxt") = PlanYearsTxt.Text
            Session("SearchTxt") = ""
            Session("SearchTxt") = " and PlanYear = " & PlanYearsTxt.Text
        Else
            ' PlanYearsTxt.Text = Session("PlanYearTxt")
        End If
    End Sub

    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs) Handles WebGrid1.InitializeDataSource
        If Session("SearchTxt") = "" Then
            Exit Sub
        End If
        Dim adapter As New MasterDataSetTableAdapters.ProductsTableAdapter
        Dim dtTable As New MasterDataSet.ProductsDataTable

        Dim sql As String = "select * from Products "
        sql += " WHERE 1 = 1 "
        sql += Session("SearchTxt") '" and PlanYear = " & Session("SearchTxt")

        sql += " ORDER BY ProductCode, PlanYear "


        adapter.SearchProducts(dtTable, sql)
        'Response.Write(sql)

        Session("count_data") = dtTable.Rows.Count

        dtTable.Columns("ProductCode").AllowDBNull = True
        dtTable.Columns("PlanYear").AllowDBNull = True
        dtTable.Columns("Description").AllowDBNull = True

        e.DataSource = dtTable
    End Sub

    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        ' don't edit column
        ' e.Row.Cells(0).ForceNoEdit = True
        e.Row.Cells(2).ForceNoEdit = True
        'e.Row.Cells(2).ForceNoEdit = True
        'e.Row.Cells(3).ForceNoEdit = True
        e.Row.Cells(5).ForceNoEdit = True
    End Sub

    Protected Sub WebGrid1_AddRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        Dim adapter As New MasterDataSetTableAdapters.ProductsTableAdapter
        ' Dim id As Decimal
        'id = adapter.InsertFCenter(e.Row.Cells(0).Value, e.Row.Cells(1).Value, e.Row.Cells(2).Value, Date.Now, Session("users"))

        Dim check = adapter.CheckExistProducts(e.Row.Cells(1).Value, e.Row.Cells(2).Value)

        If Val(check) <> 0 Then
            WebGrid1.ClearCachedDataSource()
            WebGrid1.ClientAction.Refresh()
            Throw New ArgumentException("ข้อมูลผลิตภัณฑ์ซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")
        Else
            ' Throw New ArgumentException(e.Row.Cells(1).Value)
            Dim id As Decimal
            id = adapter.InsertProducts(e.Row.Cells(1).Value, e.Row.Cells(2).Value, e.Row.Cells(3).Value, e.Row.Cells(4).Value, Date.Now, Session("users"))
            Dim dr As DataRow = e.Row.DataRow
            dr("id") = id

            logger.Info("User " & Session("users") & " insert product " & e.Row.Cells(1).Value)
        End If
        WebGrid1.ClientAction.Refresh()
        'Dim dr As DataRow = e.Row.DataRow
        'dr("FundCenterCode") = id
    End Sub

    Protected Sub WebGrid1_UpdateRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        Dim id As String
        id = e.Row.KeyValue

        Dim adapter As New MasterDataSetTableAdapters.ProductsTableAdapter
        Dim check = adapter.ChechExistUpdate(e.Row.Cells(1).Value, e.Row.Cells(2).Value, id)

        If Val(check) = 1 Then
            WebGrid1.ClearCachedDataSource()
            WebGrid1.ClientAction.Refresh()
            Throw New ArgumentException("ข้อมูลผลิตภัณฑ์ซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")
        Else
            Dim adap2 As New MasterDataSet2TableAdapters.checkuseTableAdapter
            Dim dt As MasterDataSet.ProductsDataTable
            dt = adapter.GetDataByID(Val(id))
            Dim oldcode As String = dt.Item(0).ProductCode
            Dim oldyear As Integer = dt.Item(0).PlanYear
            Dim used As Integer = adap2.CheckProduct(oldcode, oldyear)
            If oldcode.Trim = e.Row.Cells(1).Value.ToString.Trim And oldyear = Val(e.Row.Cells(2).Value) Then
                used = 0
            End If
            If used = 0 Then
                adapter.UpdateProducts(e.Row.Cells(1).Value, e.Row.Cells(2).Value, e.Row.Cells(3).Value, Date.Now, Session("users"), e.Row.Cells(4).Value, id)
                logger.Info("User " & Session("users") & " update product " & e.Row.Cells(1).Value)
            Else
                WebGrid1.ClientAction.Refresh()
                WebGrid1.ClientAction.Alert("ไม่สามารแกไขข้อมูลได้เนื่องจากข้อมูลถูกนำไปใช้งานแล้ว")
            End If
        End If
    End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        If e.Action = PostBackAction.Custom Then
            Dim type As String = TryCast(Request("type"), String)
            Dim Deletet As String = TryCast(Request("id"), String)
            Dim code As String = TryCast(Request("code"), String)
            Dim yr As String = TryCast(Request("year"), String)

            Dim adapter As New MasterDataSetTableAdapters.ProductsTableAdapter
            Dim adap2 As New MasterDataSet2TableAdapters.checkuseTableAdapter
            ' MsgBox(Deletet)
            Try
                Dim used As Integer = adap2.CheckProduct(code, Val(yr))
                If type = "1" Then
                    If used = 0 Then
                        adapter.DeleteProducts(Deletet)
                        logger.Info("User " & Session("users") & " delete product " & Deletet)
                    Else
                        WebGrid1.ClientAction.Alert("ไม่สามารลบข้อมูลได้เนื่องจากข้อมูลถูกนำไปใช้งานแล้ว")
                    End If

                ElseIf type = "2" Then
                    If used = 0 Then
                        For Each keyValue As String In WebGrid1.RootTable.GetCheckedRows()
                            adapter.DeleteProducts(keyValue)
                            logger.Info("User " & Session("users") & " delete product " & keyValue)
                        Next keyValue
                    Else
                        WebGrid1.ClientAction.Alert("ไม่สามารลบข้อมูลบางรายการได้เนื่องจากข้อมูลถูกนำไปใช้งานแล้ว")
                    End If
                End If
            Catch ex As Exception
                WebGrid1.ClientAction.Alert("ไม่สามารลบข้อมูลได้กรุณาติดต่อผู้ดูแลระบบ")
            End Try

            WebGrid1.ClientAction.Refresh()
        End If

    End Sub
    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click
        Session("SearchTxt") = ""
        If PlanYearsTxt.Text <> "" Then
            Session("SearchTxt") += " and PlanYear = " & PlanYearsTxt.Text
        End If
        If CodeTxt.Text.Trim <> "" Then
            Session("SearchTxt") += " and ProductCode like '%" & CodeTxt.Text.Trim & "%'"
        End If
        If SearchTxt.Text.Trim <> "" Then
            Session("SearchTxt") += " and Description like '%" & SearchTxt.Text.Trim & "%'"
        End If
        If Trim(ProductTypeGroupCombo.Value) <> "" Then
            Session("SearchTxt") += " and ProductType = '" & ProductTypeGroupCombo.Value & "'"
        End If
        ' Response.Write(Session("SearchTxt"))
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
        t_countdata.Visible = True
        If Val(Session("count_data")) > 0 Then
            L_CountData.Text = FormatNumber(Val(Session("count_data")), 0) & " รายการ"
        Else
            L_CountData.Text = "ไม่พบข้อมูลที่ค้นหา"
        End If
    End Sub

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
    End Sub

End Class
